package com.zzhao.activity;

import com.zzhao.calulator.Calulator;
import com.zzhao.zzhao.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button[] button;
	private StringBuffer sb=new StringBuffer();
	double result=0;
	private ImageButton ig;
	private int count=0 ;
	private static final int COUNT=2,SIZE=30;
	
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnIntil();
        
    }
    
    class listener1 implements OnClickListener{

		public void onClick(View v) {
			result=0;
			count=0;
			Button button =(Button) v;
			if(sb.length()<SIZE)
				sb.append(button.getText());
			TextView tv;
			tv=(TextView) findViewById(R.id.tv);
			tv.setText(sb.toString());
		}
    	
    }
    
    class listener2 implements OnClickListener{

		public void onClick(View v) {
			count=0;
			Button button =(Button) v; 
			TextView tv=(TextView) findViewById(R.id.tv);
			if(result!=0){
				String Result=result+"";
				sb.append(Result);
				result=0;
			}
			if(sb.length()<SIZE)
				sb.append(button.getText());
			tv.setText(sb.toString());
		}
    	
    }
    
    public void btnIntil(){
    	button=new Button[19];
    	button[0]=(Button)findViewById(R.id.btn0);
    	button[1]=(Button)findViewById(R.id.btn1);
    	button[2]=(Button)findViewById(R.id.btn2);
    	button[3]=(Button)findViewById(R.id.btn3);
    	button[4]=(Button)findViewById(R.id.btn4);
    	button[5]=(Button)findViewById(R.id.btn5);
    	button[6]=(Button)findViewById(R.id.btn6);
    	button[7]=(Button)findViewById(R.id.btn7);
    	button[8]=(Button)findViewById(R.id.btn8);
    	button[9]=(Button)findViewById(R.id.btn9);
    	button[10]=(Button)findViewById(R.id.btn10);
    	button[11]=(Button)findViewById(R.id.btn11);
    	button[12]=(Button)findViewById(R.id.btn12);
    	button[13]=(Button)findViewById(R.id.btn13);
    	button[14]=(Button)findViewById(R.id.btn14);
    	button[15]=(Button)findViewById(R.id.btn15);
    	button[16]=(Button)findViewById(R.id.btn16);
    	button[17]=(Button)findViewById(R.id.btn17);
    	button[18]=(Button)findViewById(R.id.btn19);
    	ig=(ImageButton) findViewById(R.id.btn18);
    	
    	for(int i=0;i<10;i++){
    		button[i].setOnClickListener(new listener1());
    	}
    	for(int i=11;i<15;i++){
    		button[i].setOnClickListener(new listener2());
    	}
    	for(int i=15;i<17;i++){
    		button[i].setOnClickListener(new listener1());
    	}
    	button[10].setOnClickListener(new OnClickListener() {
	
			public void onClick(View v) {
				count=0;
				TextView tv=(TextView) findViewById(R.id.tv);
				if(sb.length()!=0){
					Calulator test=new Calulator();
					sb.append("#");
					result = test.EvaluateExpression(sb.toString());	
					String Result=result+"";
					tv.setText(Result);
					sb.delete(0, sb.length());
				}else tv.setText("0");
				
			}
		});
    	button[17].setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				count=0;
				TextView tv=(TextView) findViewById(R.id.tv);
				if(sb.length()>1){
					sb.delete(sb.length()-1, sb.length());
					tv.setText(sb.toString());
				}else{
					sb.delete(0, 1);
					tv.setText("0");
				}
			}
		});
    	button[18].setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				count=0;
				result=0;
				Button button =(Button) v; 
				if(sb.length()<SIZE)
					sb.append(button.getText());
				TextView tv;
				tv=(TextView) findViewById(R.id.tv);
				tv.setText(sb.toString());
				
			}
		});
    	ig.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				count++;
				result=0;
				sb.delete(0, sb.length());
				TextView tv=(TextView) findViewById(R.id.tv);
				tv.setText("0");
				if(count==COUNT)
					finish();
			}
		});
    
    }
}
